package com.wiscamp.ninechapters.panels.adapter.resources;

import com.wiscamp.ninechapters.panels.application.command.*;
import com.wiscamp.ninechapters.panels.application.services.UserAppService;
import com.wiscamp.ninechapters.panels.application.vo.*;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.application.command.DisableCommand;
import cube.ddd.application.command.EnableCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.common.query.PagedQuery;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.HashMap;

/**
 * 用户服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/ControlPanels")
@Tag(name = "控制面板-用户信息")
public class UserController extends BaseController {

    private final UserAppService userAppService;

    public UserController(UserAppService userAppService) {
        this.userAppService = userAppService;
    }

    @GetMapping("Users/{userId}")
    @Operation(summary = "通过编号获取用户")
    @HandleResourceException
    public RestResponse<UserVO> getUser(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                        @PathVariable("userId") long userId) {
        return ResourceUtils.getById(request, response, userId, userAppService::getUser);
    }


    @GetMapping("Applications/{appId}/Users/Code/{userCode}")
    @Operation(summary = "通过代码获取用户")
    @HandleResourceException
    public RestResponse<UserVO> getUserByCode(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                              @PathVariable("appId") long appId, @PathVariable("userCode") String userCode) {
        return ResourceUtils.getByIdAndText(request, response, appId, userCode, userAppService::getUserByCode);
    }

    @GetMapping("Applications/{appId}/Users/Name/{userName}")
    @Operation(summary = "通过名称获取用户列表")
    @HandleResourceException
    public RestResponse<Collection<UserVO>> getUsersByName(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                           @PathVariable("appId") long appId, @PathVariable("userName") String userName) {
        return ResourceUtils.getListByIdAndText(request, response, appId, userName, userAppService::getUsersByName);
    }

    @PostMapping("Users/Query")
    @Operation(summary = "分页获取用户列表")
    @HandleResourceException
    public RestResponse<Collection<UserVO>> getUsersByQuery(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                            @RequestBody PagedQuery query) {
        return ResourceUtils.getPagedObjectsByQuery(request, response, query, userAppService::getUsersByQuery);
    }

    @GetMapping("Users/{userId}/Menus")
    @Operation(summary = "通过编号获取用户关联的菜单")
    @HandleResourceException
    public RestResponse<Collection<MenuVO>> getMenusByUser(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                           @PathVariable("userId") long userId) {
        return ResourceUtils.getById(request, response, userId, userAppService::getMenusByUser);
    }

    @GetMapping("Users/{userId}/Permissions")
    @Operation(summary = "通过编号获取用户关联的权限")
    @HandleResourceException
    public RestResponse<Collection<PermissionVO>> getPermissionsByUser(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                       @PathVariable("userId") long userId) {
        return ResourceUtils.getById(request, response, userId, userAppService::getPermissionsByUser);
    }

    @GetMapping("Users/{userId}/Groups")
    @Operation(summary = "通过编号获取用户关联的用户组")
    @HandleResourceException
    public RestResponse<Collection<GroupVO>> getGroupsByUser(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                             @PathVariable("userId") long userId) {
        return ResourceUtils.getById(request, response, userId, userAppService::getGroupsByUser);
    }

    @GetMapping("Users/{userId}/Roles")
    @Operation(summary = "通过编号获取用户关联的角色（包括用户组角色）")
    @HandleResourceException
    public RestResponse<Collection<RoleVO>> getRolesByUser(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                           @PathVariable("userId") long userId) {
        return ResourceUtils.getById(request, response, userId, userAppService::getRolesByUser);
    }

    @PostMapping("Users")
    @Operation(summary = "创建用户")
    @HandleResourceException
    public RestResponse<UserVO> createUser(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody CreateUserCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, userAppService::createUser);
    }

    @PostMapping("SignUp")
    @Operation(summary = "通过邮箱创建用户")
    @HandleResourceException
    public RestResponse<UserVO> signUpByEmail(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                              @RequestBody SignUpByEmailCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, userAppService::signUpByEmail);
    }

    @PostMapping("Password/Forgot")
    @Operation(summary = "通过邮箱发送用户新口令")
    @HandleResourceException
    public RestResponse<Boolean> forgotPassword(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                              @RequestBody ForgotPasswordCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, userAppService::forgotPassword);
    }

    @PutMapping("Users")
    @Operation(summary = "更新用户")
    @HandleResourceException
    public RestResponse<UserVO> updateUser(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody UpdateUserCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, userAppService::updateUser);
    }

    @PutMapping("Users/Actions/Delete")
    @Operation(summary = "通过编号删除用户")
    @HandleResourceException
    public RestResponse<Boolean> deleteUser(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, userAppService::deleteUser);
    }

    @PutMapping("Users/Actions/Enable")
    @Operation(summary = "通过编号启用用户")
    @HandleResourceException
    public RestResponse<Boolean> enableUser(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody EnableCommand command) {
        return ResourceUtils.enable(request, response, command, userAppService::enableUser);
    }

    @PutMapping("Users/Actions/Disable")
    @Operation(summary = "通过编号禁用用户")
    @HandleResourceException
    public RestResponse<Boolean> disableUser(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody DisableCommand command) {
        return ResourceUtils.disable(request, response, command, userAppService::disableUser);
    }

    @PostMapping("Users/Actions/Bind/Role")
    @Operation(summary = "绑定用户角色关系")
    @HandleResourceException
    public RestResponse<Boolean> bindUserRole(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody BindUnbindUserRoleCommand command) {
        var success = userAppService.bindUserRole(command);
        return ResourceUtils.getOkResponse(request, response, success);
    }

    @PostMapping("Users/Actions/Unbind/Role")
    @Operation(summary = "解绑用户角色关系")
    @HandleResourceException
    public RestResponse<Boolean> unbindUserRole(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody BindUnbindUserRoleCommand command) {
        var success = userAppService.unbindUserRole(command);
        return ResourceUtils.getOkResponse(request, response, success);
    }

    /*@GetMapping("Users/Actions/Encrypt/Password/{password}")
    @Operation(summary = "加密口令")
    @HandleResourceException
    public RestResponse<String> encryptUserPassword(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                @PathVariable("password") String password) {
        var encryptedPassword = userAppService.encryptUserPassword(password);
        return ResourceUtils.getOkResponse(request, response, encryptedPassword);
    }*/

    @PostMapping("Users/Names")
    @Operation(summary = "通过用户编号列表获取用户名列表")
    @HandleResourceException
    public RestResponse<HashMap<Long, String>> getNames(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                        @RequestBody Collection<Long> userIds) {
        var result = userAppService.getUserNames(userIds);
        return ResourceUtils.getOkResponse(request, response, result);
    }
}
